<?php TPL::output('m/header.tpl.htm'); ?>
<style type="text/css">
	.ckeditor-html5-video video{
    width: 100%;
}
</style>
<?php if ($this->redirect_message) { ?>
    <?php foreach ($this->redirect_message AS $key => $message) { ?>
        <div class="alert alert-warning"><?php echo $message; ?></div>
    <?php } ?>
<?php } ?>

<style>
    .markitup-box br:first-child{
        display: block;
    }
    .upload-form {
        position: relative;
        width: 0;
        height: 0;
        opacity: 0;
        filter: alpha(opacity=0);
    }
</style>
<!-- 内容 -->
<div style="background: #f8f8f8" class="container padding-0">
    <!-- 话题bar -->
    <div class="aw-topic-bar clearfix" data-id="<?php echo $this->question_info['question_id']; ?>" data-type="question"
         data-elite="<?php echo $this->question_info['elite']; ?>"
         data-stickie="<?php echo $this->question_info['stickie']; ?>">
        <div class="tag-bar">
            <?php if ($this->question_topics) { ?>
                <?php foreach ($this->question_topics as $key => $val) {
                    if ($key > 2) {
                        break;
                    } ?>
                    <span class="topic-tag" data-id="<?php echo $val['topic_id'] ?>">
        							<a class="text"
                                       href="m/topic/<?php echo $val['topic_id']; ?>"><?php echo $val['topic_title']; ?></a>
        						</span>
                <?php } ?>
            <?php } ?>
            <?php if ($this->user_id AND !$this->question_info['lock']) { ?>
                <a class="icon icon-inverse aw-add-topic-box"><i
                            class="icon icon-edit"></i>
                </a>
            <?php } ?>
        </div>
    </div>
    <!-- end 话题bar -->


    <div class="post_box">
        <div class="post_title">
            <h2><?php echo $this->question_info['question_content']; ?></h2>
        </div>
        <div class="author_text">
            <?php if(!$this->question_info['anonymous']){ ?>
            <dl>
                <dt><a href="people/<?php echo $this->question_info['user_info']['url_token']; ?>"><img src="<?php echo get_avatar_url($this->question_info['published_uid'], 'max')?>"></a></dt>
                <dd>
                    <span><?php echo $this->question_info['user_info']['user_name']; ?></span><label><?php echo date_friendly($this->question_info['add_time'], 604800, 'Y-m-d')?></label>
                </dd>
            </dl>
            <?php }else{ ?>
            <dl>
                <dt><a href="javascript:;"><img src="/static/common/avatar-max-img.png"></a></dt>
                <dd>
                    <span>匿名用户</span><label><?php echo date_friendly($this->question_info['add_time'], 604800, 'Y-m-d')?></label>
                </dd>
            </dl>
            <?php } ?>
        </div>
        <div class="Rich_text">
            <?php echo $this->question_info['question_detail']; ?>
            <?php if ($this->question_info['attachs']) { ?>
                <div class="aw-upload-img-list">
                    <?php foreach ($this->question_info['attachs'] AS $key => $attach) { ?>
                        <?php if ($attach['is_image'] AND !in_array($attach['id'], $this->question_info['attachs_ids'])) { ?>
                            <a href="<?php echo $attach['attachment']; ?>" target="_blank">
                                <img src="<?php echo $attach['attachment']; ?>"
                                     alt="<?php echo $attach['attach_name']; ?>" class="img-thumbnail"/></a>
                        <?php } ?>
                    <?php } ?>
                </div>
            <?php } ?>
            <?php if ($this->question_info['attachs']) { ?>
                <ul class="aw-upload-file-list">
                    <?php foreach ($this->question_info['attachs'] AS $attach) { ?>
                        <?php if (!$attach['is_image'] AND !in_array($attach['id'], $this->question_info['attachs_ids'])) { ?>
                            <li>
                                <a href="<?php echo download_url($attach['file_name'], $attach['attachment']); ?>"><i
                                            class="icon icon-attach"></i> <?php echo $attach['file_name']; ?></a>
                            </li>
                        <?php } ?>
                    <?php } ?>
                </ul>
            <?php } ?>

            <?php if ((!$this->question_info['lock'] AND ($this->question_info['published_uid'] == $this->user_id OR $this->user_info['permission']['edit_question'])) OR $this->user_info['permission']['is_administortar'] OR $this->user_info['permission']['is_moderator']) { ?>
                <a href="m/publish/<?php echo $this->question_info['question_id']; ?>" class="icon icon-inverse"> <i
                            class="icon icon-edit"></i> <?php _e('编辑'); ?>
                </a>
            <?php } ?>
        </div>
    </div>


        <?php if ($this->user_id) { ?>
            <div class="mod-footer aw-question-detail">
                <div class="meta">
                    <a class="aw-invite-replay btn-mini"><i class="icon icon-inviteask"></i> <?php _e('邀请回复'); ?></a>
                    <a style="font-size: 12px" class="btn btn-success btn-mini pull-right <?php if ($this->question_focus) { ?> active<?php } ?>"
                       onclick="AWS.User.follow($(this), 'question', <?php echo $this->question_info['question_id']; ?>,'<?php echo new_post_hash();?>');"><?php if ($this->question_focus) { ?><?php _e('取消关注'); ?><?php } else { ?><?php _e('关注'); ?><?php } ?></a>
                </div>
                <div class="aw-invite-box collapse">
                    <div class="mod-head users-list" id="question-users-list">
                        <?php if ($this->invite_users) { ?>
                            <span class="color-999"><?php _e('已邀请'); ?> :</span>
                            <?php foreach ($this->invite_users as $key => $val) { ?>
                                <a href="people/<?php echo $val['url_token']; ?>" data-id="<?php echo $val['uid']; ?>">
                                    <img class="img" width="20" src="<?php echo get_avatar_url($val['uid'], 'max'); ?>"/>
                                </a>
                            <?php } ?>
                        <?php } ?>
                    </div>
                    <div class="mod-body">
                        <input type="text" class="form-control" name=""/>
                        <i class="icon icon-search"></i>
                        <div class="aw-dropdown-list collapse">
                            <ul></ul>
                        </div>
                    </div>
                </div>
            </div>
        <?php } ?>
    </div>
    <!-- end 问题详情 -->

    <!-- 回复列表 -->
    <div class="aw-replay-list" style="background: #f8f8f8;">
        <?php if ($this->best_answer) { ?>
            <div class="hot_reply">
                <h4><span>最佳回复</span><a class="tag"><img src="<?php echo G_STATIC_URL ?>/mobile/images/sq.png"><i>收起</i></a>
                </h4>
                <div class="reply_text">
                    <dl>
                        <dt>
                            <a href="<?php if ($this->best_answer['anonymous']) { ?>javascript:;<?php } else { ?>m/people/<?php echo $this->best_answer['uid']; ?><?php } ?>">
                            <img src="<?php if ($this->best_answer['anonymous']) { ?><?php echo G_STATIC_URL; ?>/common/avatar-mid-img.png<?php } else { ?><?php echo get_avatar_url($this->best_answer['uid'], 'max'); ?><?php } ?>"/>
                            </a>
                        </dt>
                        <dd>
                            <h3>
                                <a href="<?php if ($this->best_answer['anonymous']) { ?>javascript:;<?php } else { ?>m/people/<?php echo $this->best_answer['uid']; ?><?php } ?>"><?php if ($this->best_answer['anonymous']) { ?><?php _e('匿名用户'); ?><?php } else { ?><?php echo $this->best_answer['user_info']['user_name']; ?><?php } ?></a>
                                <?php if ($this->best_answer['user_info']['verified'] == 'enterprise') { ?>
                                    <img src="<?php echo G_STATIC_URL ?>/mobile/images/v.png">
                                <?php } ?>
                            </h3>
                            <p>
                                <?php echo nl2br($this->best_answer['answer_content']); ?>
                                <?php if ($this->best_answer['attachs']) { ?>
                            <div class="aw-upload-img-list">
                                <?php foreach ($this->best_answer['attachs'] AS $attach) { ?>
                                    <?php if ($attach['is_image'] AND !($this->best_answer['insert_attach_ids'] AND in_array($attach['id'], $this->best_answer['insert_attach_ids']))) { ?>
                                        <a href="<?php echo $attach['attachment']; ?>" target="_blank" data-fancybox-group="thumb" rel="lightbox">
                                            <img src="<?php echo $attach['attachment']; ?>" class="img-polaroid" alt="<?php echo $attach['attach_name']; ?>"/>
                                        </a>
                                    <?php } ?>
                                <?php } ?>
                            </div>
                            <?php } ?>
                            </p>
                            <div class="mod-footer" style="margin-top: 10px;">
                                <div class="meta">
                                    <span class="operate  <?php if ($this->best_answer['agree_status'] == 1) { ?> active<?php } ?>">
                                        <a class="answer_vote agree"
                                           <?php if ($this->user_id) { ?>onclick="AWS.User.agree_vote($(this), <?php echo $this->best_answer['answer_id']; ?>)"<?php } ?>>
                                        <i class="icon icon-agree"></i> <b><?php echo $this->best_answer['agree_count']; ?></b></a>

                                    </span>

                                    <?php if ($this->user_id) { ?>
                                        <span class="operate">
                                            <a class="aw-add-comment"
                                               data-id="<?php echo $this->best_answer['answer_id']; ?>"
                                               data-type="answer" <?php if ($this->question_info['lock'] OR !$this->user_info['permission']['publish_comment']) { ?> data-close="true"<?php } ?>><i
                                                        class="icon icon-comment"></i> <?php echo $this->best_answer['comment_count']; ?></a>
								        </span>
                                        <span class="operate <?php if($this->best_answer["user_favorited"]){ ?>active<?php } ?>">
                                            <a  onclick="AWS.User.favorite('answer', <?php echo $this->best_answer['answer_id']; ?>,this)"><i
                                                        class="icon icon-favor"></i></a>
								        </span>
                                    <?php } ?>

                                    <?php if (($this->user_info['permission']['is_administortar'] OR $this->user_info['permission']['is_moderator']) OR ($this->best_answer['uid'] == $this->user_id AND ((TIMESTAMP - $this->best_answer['add_time']) < (get_setting('answer_edit_time') * 60) OR (!get_setting('answer_edit_time'))))) { ?>
                                        <span class="operate">
									<a class="text-color-999" href="javascript:;"
                                       onclick="AWS.dialog('commentEdit', {answer_id:<?php echo $this->best_answer['answer_id']; ?>,attach_access_key:'<?php echo $this->attach_access_key; ?>'});"><i
                                                class="icon icon-edit"></i></a>
								</span>
                                    <?php } ?>
                                </div>
                            </div>
                        </dd>
                        <div class="clear"></div>
                    </dl>
                </div>
            </div>
        <?php } ?>


        <div class="hot_reply">
            <h4><span>热门回复</span><a class="tag"><img src="<?php echo G_STATIC_URL ?>/mobile/images/sq.png"><i>收起</i></a>
            </h4>
            <div class="reply_text">
                <a class="aw-load-more" onclick="loadAnswer('hot',1,this)"><i class="icon icon-loading icon-spin"></i> <span>更多</span></a>
            </div>
        </div>
        <div class="hot_reply">
            <h4><span>回复</span><a class="tag"><img src="<?php echo G_STATIC_URL ?>/mobile/images/sq.png"><i>收起</i></a>
            </h4>
            <div class="reply_text">
                <a class="aw-load-more" onclick="loadAnswer('normal',1,this)"><i class="icon icon-loading icon-spin"></i> <span>更多</span></a>
            </div>
        </div>

    </div>
    <!-- end 回复列表 -->


    <!-- 回复box -->
    <div  class="aw-replay-box question" style="background: white;padding-bottom: 49px">
        <a name="answer_form"></a>

        <?php if (!$this->user_id) { ?>
            <a class="aw-load-more no-border" id="load_index_actions" href="account/login/"><?php _e('登录进行回复'); ?></a>
        <?php } else if ($this->question_info['lock']) { ?>
            <p align="center"><?php _e('该问题目前已经被锁定, 无法添加新回复'); ?></p>
        <?php } else if ($this->user_answered) { ?>
            <p align="center"><?php _e('一个问题只能回复一次'); ?><?php if (get_setting('answer_edit_time')) { ?>, <?php _e('你可以在发言后 %s 分钟内编辑回复过的内容', get_setting('answer_edit_time')); ?><?php } ?></p>
        <?php } else if ((get_setting('answer_self_question') == 'N') && ($this->user_id == $this->question_info['published_uid'])) { ?>
            <p align="center"><?php _e('不能回复自己发布的问题, 你可以修改问题内容'); ?></p>
        <?php } else { ?>
            <form action="question/ajax/save_answer/" onsubmit="return false;" method="post" id="answer_form"
                  class="question_answer_form">
                <input type="hidden" name="attach_access_key" value="<?php echo $this->attach_access_key; ?>"/>
                <input type="hidden" name="post_hash" value="<?php echo new_post_hash(); ?>"/>
                <input type="hidden" name="question_id" value="<?php echo $this->question_info['question_id']; ?>"/>

                <div class="mod-head">
                    <a href="m/people/"><img class="img" width="20" style="border-radius: 50%"
                                             src="<?php echo get_avatar_url($this->user_id, 'max'); ?>" alt=""/></a>
                    <i style="font-size: 12px;"><?php echo $this->user_info['user_name']; ?></i>
                    <span class="pull-right" style="font-size: 12px;line-height: 20px">
						<?php if (!$this->question_focus) { ?>
                            <label>
							<input type="checkbox" checked="checked" value="1" name="auto_focus"/> <?php _e('关注问题'); ?>
						</label>
                        <?php } ?>
                        <?php if (get_setting('anonymous_enable') == 'Y') { ?>
                            <label>
							<input type="checkbox" value="1" name="anonymous"/> <?php _e('匿名回复'); ?>
						</label>
                        <?php } ?>
					</span>
                </div>
                <div class="mod-body">
                    <div id="editorContainer"></div>
                    <input type="file" id="inputFile" style="display: none;">
                    <textarea name="answer_content" style="display: none;"><?php echo htmlspecialchars($this->draft_content['message']); ?></textarea>
                </div>
                <div class="mod-footer">
                    <a class="btn btn-success btn-normal pull-right" style="background: #38adff"
                       onclick="fabu()"><?php _e('回复'); ?></a>
                </div>
            </form>
        <?php } ?>
    </div>
    <!-- end 回复box -->

</div>
<!-- end 内容 -->

<script src="<?php echo G_STATIC_URL; ?>/mobile/js/zx-editor.min.js"></script>
<script type="text/javascript">
    var QUESTION_ID = <?php echo $this->question_info['question_id'];?>;
    var ATTACH_ACCESS_KEY = '<?php echo $this->attach_access_key; ?>';
    var ANSWER_EDIT_TIME = <?php echo get_setting('answer_edit_time'); ?>;
    var USER_ANSWERED = '<?php echo $this->user_answered; ?>';
    var TEMPLATE_MES = '';
    var PUBLISH_TYPE = 'answer';

    if($('#editorContainer').length>0){
        var zxEditor = new ZxEditor('#editorContainer', {
            // 顶部偏移距离
            // demo有顶部导航栏，高度44
            top: 44,
            // 编辑框左右边距
            padding: 0,
            bottom:10
        });
        $('.zxeditor-content-wrapper').css('margin-bottom',0)
        $('.zxeditor-content-wrapper').css('overflow-y','scroll')
        $('.zxeditor-content-wrapper').css('border','0.5px solid #eee')

        var $inputFile = document.querySelector('#inputFile')
        zxEditor.on('select-picture', function () {
            // 触发input点击事件
            $inputFile.click();
        })
        var answer_content = $('textarea[name=answer_content]').val()
        zxEditor.setContent(answer_content)
        $inputFile.addEventListener('change', function (e) {
            var files = e.target.files; // 或者 $inputFile.files
            var file = files[0]
            // 上传文件至服务器
            upload({
                // 接收上传文件的服务器url
                url: '<?php echo get_js_url('/publish/ajax/attach_upload/id-answer__attach_access_key-'.$this->attach_access_key); ?>&aws_upload_file='+file.name+'&timestamp='+(new Date()).getTime(),
                file: file,
                success: function (res) {
                    try {
                        // 服务器返回的json字符串
                        res = JSON.parse(res)
                        if (!res.error) {
                            // 添加图片至编辑器内
                            zxEditor.addImage(res.thumb.replace('90x90_',''))
                            $('#inputFile').val('')
                        } else {
                            layer.open({
                                content: res.error
                                ,skin: 'msg',time: 2
                            });
                            $('#inputFile').val('')
                        }
                    } catch (e) {}
                },
                error: function (err) {
                    layer.open({
                        content: '文件上传失败'
                        ,skin: 'msg',time: 2
                    });
                    $('#inputFile').val('')
                }
            })
        })
        function fabu(){
            var html = zxEditor.getContent()
            var txt = zxEditor.removeHtmlTags(html).replace(/&nbsp;/g,'')
            if(!txt.trim()){
                layer.open({
                    content: '请输入内容'
                    ,skin: 'msg',time: 2
                });
                return false
            }
            $('textarea[name=answer_content]').val(html)
            AWS.ajax_post($('#answer_form'), AWS.ajax_processer, 'reply');
        }
        // 自动保存草稿
        zxEditor.on('change', function (type, zxeditor) {
            var html = zxeditor.getContent()
            $.post(G_BASE_URL + '/account/ajax/save_draft/?item_id=' + QUESTION_ID + '&type=answer', 'message=' + html, function (result) {
            }, 'json');
        })
    }

    $(document).ready(function () {
        var image = '<?php echo G_STATIC_URL . '/css/default/img/oauth_logo.png'?>';
        if ($(".aw-upload-img-list").length > 0) {
            image = $(".aw-upload-img-list img:first").attr("src");
        }
        <?php if($this->isShare){ ?>
        AWS.DingTalk.share(1,'<?php echo get_js_url('/m/question/'.$this->question_info['question_id'])?>','<?php echo $this->question_info['question_content']?>','<?php echo cjk_substr(str_replace(array("\r", "\n"), array(' ', ' '), strip_tags($this->question_info['question_detail'])), 0, 130, 'UTF-8', '...')?>', image);
        <?php } ?>

        var fileupload = new FileUpload('file', '.aw-upload-box .icon-pic', '.aw-replay-box .upload-container', G_BASE_URL + '/publish/ajax/attach_upload/id-answer__attach_access_key' + '-' + ATTACH_ACCESS_KEY, {
            'deleteBtnTemplate': '<a class="delete-file"><i class="icon icon-delete"></i></a>',
            'insertBtnTemplate': '<a class="insert-file"><i class="icon icon-insert"></i></a>',
            'editor': $('.textarea_content')
        })

        $(".hot_reply .tag").click(function () {
            var $e = $(this);
            if ($e.parent().next().is(":hidden")) {
                $e.parent().next().fadeIn(function () {
                    $e.find("img").attr("src", "<?php echo G_STATIC_URL ?>/mobile/images/sq.png").next().text("收起");
                });
            } else {
                $e.parent().next().fadeOut(function () {
                    $e.find("img").attr("src", "<?php echo G_STATIC_URL?>/mobile/images/open_ico.png").next().text("展开");
                });
            }
        });
        $(".hot_reply .aw-load-more").click();

        /*document.querySelector('body').addEventListener('touchend', function(e) {
            if(e.target.tagName != 'textarea') {
                document.querySelector('textarea').blur();
            }
        });*/

    });

    function loadAnswer(type,page,e) {
        var $answerList = $(e).parent();
        if (!$(e).hasClass("loading")) {
            $(e).addClass("loading");
            $.get(G_BASE_URL + '/m/ajax/'+(type=='hot'?'hot_answer_list':'answer_list')+'/', 'questionId=' + QUESTION_ID + '&page='+page, function (result) {
                $(e).remove();
                if (result && $.trim(result)) {
                    $answerList.append(result);
                }
            })
        }
    }

    function openEdit(param){
        location.href = G_BASE_URL+'/m/edit_answer/?question_id=<?php echo $_GET['id']; ?>&id='+param.answer_id+'&attach_access_key='+param.attach_access_key;
    }
</script>

<?php TPL::output('m/footer.tpl.htm'); ?>
